package com.test.task.controller;

import com.test.task.beans.Plan;
import com.test.task.beans.Task;
import com.test.task.beans.User;
import com.test.task.service.StaffService;
import com.test.task.service.StaffServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/staff")
public class StaffPlanController {
    @Autowired
    private StaffService staffService;
    @RequestMapping("aaa")
    public String aaa(){
        return null;
    }
    @RequestMapping("/removePlan")
    public String removePlan(Integer id,Model model){
        staffService.deletePlan(id);
        return "redirect:/staff/manageTask";
    }
    @RequestMapping("/listPlan")
    public String listPlan(Model model,Plan plan){
        List<Plan> plans = staffService.listPlan(plan);
        List<Task> tasks = staffService.listTask();
        model.addAttribute("plan",plan);
        model.addAttribute("tasks",tasks);
        model.addAttribute("plans",plans);
        return "/staff/selectPlans";
    }
    @RequestMapping("/manageTask")
    public String managePlan(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        String name = user.getName();
        List<Task> tasks = staffService.queryTask(user.getUsername());
        model.addAttribute("name",name);
        model.addAttribute("tasks",tasks);
        return "/staff/listTask";
    }
    @RequestMapping("/makePlan")
    public String makePlan(Integer id,HttpSession session,Model model,Plan plan,Integer flag){
        model.addAttribute("id",id);
        User user = (User) session.getAttribute("user");
        if(flag==null){
            plan.setTask_id(id);
            plan.setStaff_id(user.getUsername());
            staffService.addPlan(plan);
            return "redirect:/staff/manageTask";
        }
        return "/staff/newPlan";
    }
    @RequestMapping("/planOfTask")
    public String planOfTask(Plan plan,Model model){
        Task task = staffService.findTask(plan.getTask_id());
        List<Plan> plans = staffService.listPlan(plan);
        model.addAttribute("plans",plans);
        model.addAttribute("task",task);
        return "staff/detailedStaffHavaImplement";
    }
    @RequestMapping("/feedBack")
    public String feedback(Integer flag,Plan plan,Model model){
        if(flag==null){
            staffService.feedback(plan);
            return "redirect:/staff/manageTask";
        }else {
            plan = staffService.findPlan(plan.getId());
        }
        model.addAttribute("plan",plan);
        return "/staff/inputFeedbackMsg";
    }
}
